﻿

<?php
include "connect/connect_dsccmis.php";
mysql_query("set names tis620");
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
header("Content-type: text/xml;  charset=TIS-620");
// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0
header("Pragma: no-cache");
//XML Header
header("content-type:text/xml");

createKmlFile();
function parseToXML($htmlStr)
{ 

$xmlStr=str_replace('<','&lt;',$htmlStr); 

$xmlStr=str_replace('>','&gt;',$xmlStr); 

$xmlStr=str_replace('"','&quot;',$xmlStr); 

$xmlStr=str_replace("'",'&#39;',$xmlStr); 

$xmlStr=str_replace("&",'&amp;',$xmlStr); 

return $xmlStr; 

} 


function createKmlFile(){

		$occProvincestr = "";
		$occProvincestr1 = "";
		$occProvince = $_GET["OccProvince"];
		$occProvincexx = $_GET["OccProvince"];
		
			$occProvince90 = $_GET["occProvince90"];
			$occProvince94 = $_GET["occProvince94"];
			$occProvince95 = $_GET["occProvince95"];
			$occProvince96 = $_GET["occProvince96"];
			$occProvince00 = $_GET["occProvince00"];
			if($occProvince00!="00"){
			if($occProvince90=="90") {$occProvincestr=$occProvincestr.",'สงขลา'" 	;$occProvincestr1=$occProvincestr1. ",90"  ; }
			if($occProvince94=="94") {$occProvincestr=$occProvincestr.",'ปัตตานี'" 	;$occProvincestr1=$occProvincestr1. ",94"  ; }
			if($occProvince95=="95") {$occProvincestr=$occProvincestr.",'ยะลา'"	;$occProvincestr1=$occProvincestr1. ",95"  ; }
			if($occProvince96=="96") {$occProvincestr=$occProvincestr.",'นราธิวาส'"	;$occProvincestr1=$occProvincestr1. ",96"  ; }
			$occProvincestr=substr($occProvincestr,1);
			$occProvincestr1=substr($occProvincestr1,1);
			$occProvincestr=" and prv  in (".$occProvincestr.")";	
			$occProvincestr1=" and prov  in (".$occProvincestr1.")";	
			}				
			else	if($occProvince90 == "90"   and
			           $occProvince94 == "94"   and
			           $occProvince95 == "95"   and
			           $occProvince96 == "96"
			
			
			)
			{
			$occProvincestr='';
			$occProvincestr1='';
			}
			else
			{
			if($occProvince90 != "90") {$occProvincestr=$occProvincestr.",'สงขลา'" 	;$occProvincestr1=$occProvincestr1. ",90"  ; }
			if($occProvince94 != "94") {$occProvincestr=$occProvincestr.",'ปัตตานี'" 	;$occProvincestr1=$occProvincestr1. ",94"  ; }
			if($occProvince95 != "95") {$occProvincestr=$occProvincestr.",'ยะลา'"	;$occProvincestr1=$occProvincestr1. ",95"  ; }
			if($occProvince96 != "96") {$occProvincestr=$occProvincestr.",'นราธิวาส'"	;$occProvincestr1=$occProvincestr1. ",96"  ; }
			$occProvincestr=substr($occProvincestr,1);
			$occProvincestr1=substr($occProvincestr1,1);
			$occProvincestr=" and prv not in (".$occProvincestr.")";
			$occProvincestr1=" and prov not in (".$occProvincestr1.")";
			}
			
		$occProvincexx = $occProvincestr;

$query = "select * from gis_victim where lat is not null and lng is not null ".$occProvincestr1."  ORDER BY `gis_victim`.`prov` DESC ";

mysql_query("SET NAMES UTF8");
$result = mysql_query($query);

if (!$result) {

  die('Invalid query: ' . mysql_error());

}



header("Content-type: text/xml");


// Start XML file, echo parent node
//echo $query ;
echo '<farm>';

//while ($row = @mysql_fetch_assoc($resultA)){
//}
// Iterate through the rows, printing XML nodes for each
$id_adr_B=0;
$count=0;
$prv="ปัตตานี";
while ($row = @mysql_fetch_assoc($result)){

$id_adr_A= $row['rolplcidgen'];
//$query2="SELECT * FROM victims_6868 WHERE plcidgen_mis='".$row['plcidgen_mis']."'and type_perlos='เสียชีวิต' ";
$query1="SELECT * FROM gis_victim WHERE rolplcidgen='".$row['rolplcidgen']."'";
$query2="SELECT * FROM gis_victim WHERE rolplcidgen='".$row['rolplcidgen']."'and losbodynme='เสียชีวิต ณ ที่เกิดเหตุ' ";
$query3="SELECT * FROM gis_victim WHERE rolplcidgen='".$row['rolplcidgen']."'and losbodynme='บาดเจ็บ(รักษาตัว 8-20 วัน)' ";
$query4="SELECT * FROM gis_victim WHERE rolplcidgen='".$row['rolplcidgen']."'and losbodynme='บาดเจ็บเล็กน้อย(รักษาตัวไม่เกิน 7 วัน)' ";
$query5="SELECT * FROM gis_victim WHERE rolplcidgen='".$row['rolplcidgen']."'and losbodynme='บาดเจ็บสาหัส(รักษาตัว 20 วันขึ้นไป' ";
$query6="SELECT * FROM gis_victim WHERE rolplcidgen='".$row['rolplcidgen']."'and losbodynme='ปลอดภัย/ทรัพย์สินเสียหาย' ";

$result1 = mysql_query($query1);
$result2 = mysql_query($query2);
$result3 = mysql_query($query3);
$result4 = mysql_query($query4);
$result5 = mysql_query($query5);
$result6 = mysql_query($query6);
$num=mysql_num_rows($result1);
$count2	 = mysql_num_rows($result2);
$count3	 = mysql_num_rows($result3);
$count4	 = mysql_num_rows($result4);
$count5	 = mysql_num_rows($result5);
$count6	 = mysql_num_rows($result6);
if($id_adr_B != $id_adr_A){
$id_adr_B = $id_adr_A;
  echo '<marker ';
echo 'count="' . 			parseToXML($num) . '" ';
echo 'count2="' . 			parseToXML($count2) . '" ';
echo 'count3="' . 			parseToXML($count3) . '" ';
echo 'count4="' . 			parseToXML($count4) . '" ';
echo 'count5="' . 			parseToXML($count5) . '" ';
echo 'count6="' . 			parseToXML($count6) . '" ';
echo 'name="1' . 			parseToXML($row['vll']) . '" ';
echo 'age="' . 	     		parseToXML($row['prov_name']) . '" ';
 if("จ.นราธิวาส"==parseToXML($row['prov_name']) or"จ.ปัตตานี"==parseToXML($row['prov_name']) or"จ.สงขลา"==parseToXML($row['prov_name']) or"จ.ยะลา"==parseToXML($row['prov_name']))
{
echo 'address="'  .parseToXML($row['vill_name']) .parseToXML($row['	tam_name']).parseToXML($row['amp_name']) .	parseToXML($row['prov_name']) . '" ';
}
else if("กรุงเทพมหานคร"==parseToXML($row['prv']) )
echo 'address="' . 	     		parseToXML($row['prv']) . '" ';
else
echo 'address="' . 	     	 ' จังหวัด'.parseToXML($row['vill_name']) . '" ';

  echo 'lat="' . 			$row['lat'] . '" ';

  echo 'lng="' . 			$row['lng'] . '" ';


  echo '/>';
  $count=0;

}else
$count =$count+1;
}
//###########################################################################################################################################
$query = "select * from victims_6868 where lat is not null and lng is not null".$occProvincestr." ORDER BY `victims_6868`.`plcidgen_mis` DESC ";
$result = mysql_query($query);
if (!$result) {

  die('Invalid query: ' . mysql_error());

}

// Iterate through the rows, printing XML nodes for each
$id_adr_B=0;
$count=0;
$prv="ปัตตานี";
while ($row = @mysql_fetch_assoc($result)){
//$query1="select * from occ where id_adr_b=".$row['id_adr_b'];
//$result1 = mysql_query($query1);
//while ($row1 = @mysql_fetch_assoc($result1)){
  // ADD TO XML DOCUMENT NODE
//echo $query ;
//echo $xxx;
//echo $OccNumberzz;
//$id_adr_A= $row['id_adr_b'];
$id_adr_A= $row['plcidgen_mis'];
//$query1="SELECT COUNT(id_adr_b) FROM occ WHERE id_adr_b='".$row['id_adr_b']."'";
//$query1="SELECT COUNT(plcidgen_mis) FROM victims_6868 WHERE plcidgen_mis='".$row['plcidgen_mis']."'";
$query1="SELECT * FROM victims_6868 WHERE plcidgen_mis='".$row['plcidgen_mis']."'";
//SELECT * FROM victims_6868 WHERE plcidgen_mis='".$row['plcidgen_mis']."'and type_perlos='เสียชีวิต' "
$query2="SELECT * FROM victims_6868 WHERE plcidgen_mis='".$row['plcidgen_mis']."'and type_perlos='เสียชีวิต' ";
$query3="SELECT * FROM victims_6868 WHERE plcidgen_mis='".$row['plcidgen_mis']."'and type_perlos='ทุพพลภาพ' ";
$query4="SELECT * FROM victims_6868 WHERE plcidgen_mis='".$row['plcidgen_mis']."'and type_perlos='บาดเจ็บปานกลาง' ";
$query5="SELECT * FROM victims_6868 WHERE plcidgen_mis='".$row['plcidgen_mis']."'and type_perlos='บาดเจ็บเล็กน้อย' ";
$query6="SELECT * FROM victims_6868 WHERE plcidgen_mis='".$row['plcidgen_mis']."'and type_perlos='บาดเจ็บสาหัส' ";

$result1 = mysql_query($query1);
$result2 = mysql_query($query2);
$result3 = mysql_query($query3);
$result4 = mysql_query($query4);
$result5 = mysql_query($query5);
$result6 = mysql_query($query6);
//$row1 = @mysql_fetch_assoc($result1)
//$id_adr_B=$id_adr_A;
$num=mysql_num_rows($result1);
$count2=mysql_num_rows($result2);
$count3=mysql_num_rows($result3);
$count4=mysql_num_rows($result4);
$count5=mysql_num_rows($result5);
$count6=mysql_num_rows($result6);
$countSum=$count3+$count6;
if($id_adr_B != $id_adr_A){
$id_adr_B = $id_adr_A;
  echo '<marker ';

// echo 'id="' . 			parseToXML($row['occ_name']) . '" ';
// echo 'idfarm="' . 		parseToXML($row['type_occ_a']) . '" ';
// echo 'surnme="' .	 	parseToXML($row['occ_day']) . '" ';
// echo 'prenme="' . 		parseToXML($row['D']) . '" ';
echo 'count="' . 			parseToXML($num) . '" ';
//echo 'count2"' .			parseToXML($num) . '" ';
echo 'count2="' . 			parseToXML($count2) . '" ';
//echo 'count3="' . 		parseToXML($count3) . '" ';
echo 'count3="' . 			parseToXML($count4) . '" ';
echo 'count4="' . 			parseToXML($count5) . '" ';
//echo 'count6="' . 		parseToXML($count6) . '" ';
echo 'count5="' . 			parseToXML($countSum) . '" ';
//echo 'count6="0"';
echo 'count6="0" ';
echo 'name="2" ';
echo 'age="' . 	     		parseToXML($row['amp']) . '" ';
if("ปัตตานี"==parseToXML($row['prv']) or"ยะลา"==parseToXML($row['prv']) or"สงขลา"==parseToXML($row['prv']) or"นราธิวาส"==parseToXML($row['prv']))
{
echo 'address="'  . 'ม.'.parseToXML($row['vll']) .' ต.'.parseToXML($row['tmb']).' อ.'.parseToXML($row['amp']) .' จ.'.	parseToXML($row['prv']) . '" ';
}
else if("กรุงเทพมหานคร"==parseToXML($row['prv']) )
echo 'address="' . 	     		parseToXML($row['prv']) . '" ';
else
echo 'address="' . 	     		' จังหวัด'.parseToXML($row['prv']) . '" ';
//echo 'count="' . 		parseToXML($row1['occ_day']) . '" ';
  echo 'lat="' . 			$row['lat'] . '" ';

  echo 'lng="' . 			$row['lng'] . '" ';
//  echo 'type="' . 			$row['H'] . '" ';

  echo '/>';
  $count=0;
 // }
}else
$count =$count+1; 
}

echo $query."\n";
//echo $id_adr_A."\n";
//echo $occProvincestr;

echo '</farm>';


}

?>
